package com.kevin.minio.util;

import com.google.common.collect.Lists;
import io.minio.MinioClient;
import io.minio.Result;
import io.minio.messages.Item;

import java.io.InputStream;
import java.util.List;

/**
 * @author Kevin Liu
 * @date 2020/6/9 7:05 下午
 */

public class FileUploadClient {

    private final MinioClient minioClient;

    private final String bucketName;

    public FileUploadClient(MinioClient minioClient,String bucketName) {
        this.minioClient = minioClient;
        this.bucketName = bucketName;
    }


    public boolean putObject(String fileName,String filePath) {
        try {
            minioClient.putObject(bucketName,fileName,filePath,null);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean putObject(String fileName, InputStream inputStream) {
        try {
            minioClient.putObject(bucketName,fileName,inputStream,null);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public List<String> listObjects(){
        List<String> res = Lists.newArrayList();
        try {
            Iterable<Result<Item>> results = minioClient.listObjects(bucketName);
            for (Result<Item> result : results) {
                Item item = result.get();
                res.add(item.objectName());
            }
            return res;
        } catch (Exception ignored) {

        }
        return res;
    }



}
